//
//  TopicViewController.m
//  ThePrivateKitchen
//
//  Created by lirui on 16/1/20.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "TopicViewController.h"

#import <AFNetworking.h>

@interface TopicViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIScrollViewDelegate,passUrlDelegate,PassValueDelegate>
@property(nonatomic, retain)NSMutableArray *dataArr;
@property(nonatomic, retain)UICollectionView *collectionview;

@property(nonatomic, retain)UIView *aView;

@property(nonatomic,retain)HeadView *hView;//头部大collectionview和小collectionView
@property(nonatomic, retain)Head_2VIew *h2_view;  //人气专题


@end

@implementation TopicViewController

- (void)dealloc
{
    [_dataArr release];
    [_collectionview release];
    [_aView release];
    [_hView release];
    [_h2_view release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [ UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    
    [self AFNetworkWithFavorite];
    [self creatCollectionView];
    [self top];
    [self loadMBProgress];
 
   // [self creatSeg];
    self.navigationItem.title = @"下厨房";
  
    
    
}
#pragma mark -创建分段控制器
#pragma mark -头部区域视图
-(void)top
{
    self.aView =[[ UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH *375, HEIGHT * 1030)];
    //人气专题的View
    self.h2_view = [[ Head_2VIew alloc] initWithFrame:CGRectMake(0,HEIGHT * 340, WIDTH *375,HEIGHT * 600)];
   
    self.h2_view.delegate = self;
    self.h2_view.btndelegate = self;
    UILabel *label = [[ UILabel alloc] initWithFrame:CGRectMake(0, WIDTH*(670 +320), WIDTH *375, HEIGHT* 40)];
    label.text = @"#猜你喜欢#";
    label.textAlignment = NSTextAlignmentCenter;
    [self.aView addSubview:label];
    [_h2_view release];
    
#pragma mark -顶部视图大collectionview 和小collectionview
    self.hView = [[ HeadView alloc] initWithFrame:CGRectMake(0, 0, WIDTH*375, HEIGHT*350)];
    BLOCK1 nextblock= ^(NSString *str)
    {
       Topic_detailViewController *detail = [[Topic_detailViewController alloc] init];
        detail.string = str;
        [self.navigationController pushViewController:detail  animated:YES];
    };
    self.hView.next = nextblock;
    [self.aView addSubview:_h2_view];
      [_aView addSubview:self.hView];
    [self.hView release];
   
    [_aView release];
    
}
-(void)btn:(UIButton *)b
{
    ListViewController *list =[[ ListViewController alloc] init];
    [self.navigationController pushViewController:list animated:YES];
}
//协议方法
-(void)passWithURL:(NSString *)url
{
    TuijianViewController *tuijian =[[ TuijianViewController alloc] init];
    tuijian.string = url;
    [self.navigationController pushViewController:tuijian animated:YES];
}
-(void)passValue
{JinrituijianViewController *jinri = [[JinrituijianViewController alloc] init];
    [self.navigationController pushViewController:jinri animated:YES];
    
}
#pragma mark -猜你喜欢数据请求
-(void)AFNetworkWithFavorite
{
    NSString *str = @"http://api.meishi.cc/v5/index3.php?format=json%20?lon=4.9E-324&source=android&page=3&lathttp://api.meishi.cc/v5/index3.php?format=json?lon=&source=android&lat=&c_md5=b79c4bb9d8b83017dcaea6fff3ca35c8&format=json&=4.9E-324&c_md5=eab2fbd9643207d85c970304896de5cb&format=json&";
    [LRAFNetwork postUrlString:str body:nil response:JSON bodyStyle:RequestJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSArray *arr = responseObject[@"obj"][@"customized"][@"data"];
        self.dataArr =[ NSMutableArray array];
        self.dataArr = [ TopicModel modelHanderWithArray:arr];
    
        
        [_collectionview reloadData];
        [self.hud removeFromSuperview];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}
#pragma mark -创建collectionview
-(void)creatCollectionView

{ UICollectionViewFlowLayout  *layout = [[ UICollectionViewFlowLayout alloc] init];
    //布局设置
    //滚动方向
    layout.scrollDirection =  UICollectionViewScrollDirectionVertical;
    //cell大小
    layout.itemSize = CGSizeMake((WIDTH *375 -50*WIDTH )/2 ,HEIGHT * 120);
    //行列间距
    //行
    layout.minimumLineSpacing = WIDTH * 40;
    //列
    layout.minimumInteritemSpacing = 0;
    //边界
    layout.sectionInset = UIEdgeInsetsMake(WIDTH * 10,HEIGHT * 15,WIDTH * 50,HEIGHT * 15);
    //头部
    layout.headerReferenceSize = CGSizeMake(WIDTH * 100,HEIGHT* 1030);
     layout.footerReferenceSize = CGSizeMake(WIDTH * 300,HEIGHT * 50);
    self.collectionview =[[ UICollectionView alloc] initWithFrame:CGRectMake(0, 0,WIDTH *375 , HEIGHT *667 - 100)   collectionViewLayout:layout];
    self.collectionview .delegate = self;
    self.collectionview.dataSource = self;
    self.collectionview.backgroundColor = [UIColor whiteColor];
    _collectionview.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [self.view addSubview:self.collectionview];
    //[_collectionview setContentOffset:CGPointMake(375 * 2, 0)];
    _collectionview.pagingEnabled = NO;
    
    [_collectionview registerClass:[TopicCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    //头部区域注册
    [_collectionview registerClass: [UICollectionReusableView class]forSupplementaryViewOfKind:UICollectionElementKindSectionHeader  withReuseIdentifier:@"header"];
      [_collectionview registerClass:[ UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
    
}
//头部视图方法
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {

    
    //从重用池获取头部视图
    UICollectionReusableView *header =[ collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
    header.backgroundColor = [ UIColor whiteColor];
    
    [header addSubview:_aView];
    
    
    return header;
    }
    else
    {
        UICollectionReusableView *footer =[ collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
        footer.backgroundColor =[ UIColor whiteColor];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH *375,HEIGHT * 50)];
        label.text = @"不感兴趣? 点一下>>>";
        label.textAlignment = NSTextAlignmentCenter;
        label.font =[ UIFont systemFontOfSize:15];
        [footer addSubview:label];
        [label release];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [footer addGestureRecognizer:tap];
        [tap release];
        return footer;
        
    }

    
}
-(void)tapAction:(UITapGestureRecognizer *)t
{
    NSLog(@"11111");
    ListViewController *list =[[ ListViewController alloc] init];
    [self.navigationController pushViewController:list animated:YES];
}
#pragma mark -必须实现的协议方法
//cell数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 6;
}
//cell设置
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //获取cell
   TopicCollectionViewCell *cell = [ collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    TopicModel *m = _dataArr[ indexPath.item];
    [cell.imgview sd_setImageWithURL:[NSURL URLWithString:m.titlepic] placeholderImage:[UIImage imageNamed:@"noReply"]];
    cell.title.text = m.title;
    cell.title.textColor = [ UIColor whiteColor];
    return cell;
    
    
}
//点击方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _collectionview) {
        FavorViewController *fav = [[ FavorViewController alloc] init];
        fav.model = _dataArr[indexPath.item];
        [self.navigationController pushViewController:fav animated:YES];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
